package com.s7.pull;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.core.view.NestedScrollingChild;

import com.s7.pull.api.RefreshInternal;
import com.s7.pull.listener.OnPullListener;

/**
 * 下拉刷新、上拉加载
 */
public class PullRefreshLayout extends ViewGroup implements OnPullListener, NestedScrollingChild {

    // 波浪的高度,最大扩展高度
    protected float mMaxHeaderHeight;
    protected float mMaxFooterHeight;

    //头部的高度
    protected float mHeadHeight;

    //允许的越界回弹的高度
    protected float mOverScrollHeight;

    //子控件
    private View mChildView;

    //头部layout
    protected FrameLayout mHeadLayout;

    //整个头部
    private FrameLayout mExtraHeadLayout;
    //附加顶部高度
    private int mExHeadHeight = 0;

    // 头部和尾部View
    private RefreshInternal mHeadView;
    private RefreshInternal mFooterView;

    //设置的默认的header/footer class的完整包名+类名
    private static String HEADER_CLASS_NAME = "";
    private static String FOOTER_CLASS_NAME = "";

    //底部高度
    private float mBottomHeight;

    //底部layout
    private FrameLayout mBottomLayout;

    //是否刷新视图可见
    protected boolean isRefreshVisible = false;

    //是否加载更多视图可见
    protected boolean isLoadingVisible = false;

    //是否处于刷新状态（和isRefreshVisible区别为是否开启了enableKeepHeadWhenRefresh）
    protected boolean isRefreshing = false;
    protected boolean isLoadingMore = false;

    //是否需要加载更多,默认需要
    protected boolean enableLoadmore = true;
    //是否需要下拉刷新,默认需要
    protected boolean enableRefresh = true;

    //是否在越界回弹的时候显示下拉图标
    protected boolean isOverScrollTopShow = true;
    //是否在越界回弹的时候显示上拉图标
    protected boolean isOverScrollBottomShow = true;

    //是否隐藏刷新控件,开启越界回弹模式(开启之后刷新控件将隐藏)
    protected boolean isPureScrollModeOn = false;

    //是否自动加载更多
    protected boolean autoLoadMore = false;

    //是否开启悬浮刷新模式
    protected boolean floatRefresh = false;

    //是否允许进入越界回弹模式
    protected boolean enableOverScroll = true;

    //是否在刷新或者加载更多后保持状态
    protected boolean enableKeepIView = true;

    //是否在越界且处于刷新时直接显示顶部
    protected boolean showRefreshingWhenOverScroll = true;

    //是否在越界且处于加载更多时直接显示底部
    protected boolean showLoadingWhenOverScroll = true;

//    private CoContext cp;
    private final int mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    //设置手势动作的监听器
    private OnPullListener pullListener = this;

//    private final NestedScrollingChildHelper mChildHelper;

    public PullRefreshLayout(Context context) {
        super(context);
    }

    public PullRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 重写 onFinishInflate 来完成 smart 的特定功能
     * 1.智能寻找 Xml 中定义的 Content、Header、Footer
     */
    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        final int count = super.getChildCount();
        if (count > 3) {
            throw new RuntimeException("最多只支持3个子View");
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    /**
     * android 24及以上ViewGroup会继续往上派发， 23以及以下直接返回false
     *  低于5.0的机器和RecyclerView配合工作时，部分机型会调用这个方法，
     *  但是ViewGroup并没有实现这个方法，会报错，这里catch一下
     * @param target
     * @param velocityX
     * @param velocityY
     * @param consumed
     * @return
     */
    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        try {
            return super.onNestedFling(target, velocityX, velocityY, consumed);
        } catch (Throwable e) {
        }
        return false;
    }

    @Override
    public void onPullDown(PullRefreshLayout layout, float fraction) {

    }

    @Override
    public void onPullUp(PullRefreshLayout layout, float fraction) {

    }

    @Override
    public void onPullDownReleasing(PullRefreshLayout layout, float fraction) {

    }

    @Override
    public void onPullUpReleasing(PullRefreshLayout layout, float fraction) {

    }

    @Override
    public void onRefresh(PullRefreshLayout layout) {

    }

    @Override
    public void onLoad(PullRefreshLayout layout) {

    }

    @Override
    public void onFinishRefresh() {

    }

    @Override
    public void onFinishLoad() {

    }

    @Override
    public void onRefreshCanceled() {

    }

    @Override
    public void onLoadmoreCanceled() {

    }
}
